package com.iyundao.io.mapper;

import com.iyundao.io.model.SysDept;
import com.iyundao.io.model.SysDeptProvider;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface SysDeptSqlMapper {
    @Select({
        "select sys_dept.* from sys_dept ",
                "LEFT JOIN user_dept on user_dept.did = sys_dept.sid",
                "where sys_dept.flag = '1' and user_dept.uid = #{userId,jdbcType=CHAR}"
    })
    @Results({
            @Result(column="sid", property="sid", jdbcType=JdbcType.CHAR, id=true),
            @Result(column="real_name", property="realName", jdbcType=JdbcType.VARCHAR),
            @Result(column="show_name", property="showName", jdbcType=JdbcType.VARCHAR),
            @Result(column="parent_id", property="parentId", jdbcType=JdbcType.CHAR),
            @Result(column="detailed", property="detailed", jdbcType=JdbcType.VARCHAR),
            @Result(column="flag", property="flag", jdbcType=JdbcType.VARCHAR),
            @Result(column="create_date", property="createDate", jdbcType=JdbcType.TIMESTAMP),
            @Result(column="create_id", property="createId", jdbcType=JdbcType.CHAR),
            @Result(column="orde_by", property="ordeBy", jdbcType=JdbcType.TINYINT)
    })
    SysDept selectByUserId(String userId);

    @Select({
            "select * from sys_dept where parent_id = #{parentId,jdbcType=CHAR} ORDER BY " +
                    "sys_dept.orde_by ASC"
    })
    @Results({
            @Result(column="sid", property="sid", jdbcType=JdbcType.CHAR, id=true),
            @Result(column="real_name", property="realName", jdbcType=JdbcType.VARCHAR),
            @Result(column="show_name", property="showName", jdbcType=JdbcType.VARCHAR),
            @Result(column="parent_id", property="parentId", jdbcType=JdbcType.CHAR),
            @Result(column="detailed", property="detailed", jdbcType=JdbcType.VARCHAR),
            @Result(column="flag", property="flag", jdbcType=JdbcType.VARCHAR),
            @Result(column="create_date", property="createDate", jdbcType=JdbcType.TIMESTAMP),
            @Result(column="create_id", property="createId", jdbcType=JdbcType.CHAR),
            @Result(column="orde_by", property="ordeBy", jdbcType=JdbcType.TINYINT)
    })
    List<SysDeptProvider> selectTreeByParentId(String parentId);

    @Select({
            "select * from sys_dept where sid = #{sid,jdbcType=CHAR}"
    })
    @Results({
            @Result(column="sid", property="sid", jdbcType=JdbcType.CHAR, id=true),
            @Result(column="real_name", property="realName", jdbcType=JdbcType.VARCHAR),
            @Result(column="show_name", property="showName", jdbcType=JdbcType.VARCHAR),
            @Result(column="parent_id", property="parentId", jdbcType=JdbcType.CHAR),
            @Result(column="detailed", property="detailed", jdbcType=JdbcType.VARCHAR),
            @Result(column="flag", property="flag", jdbcType=JdbcType.VARCHAR),
            @Result(column="create_date", property="createDate", jdbcType=JdbcType.TIMESTAMP),
            @Result(column="create_id", property="createId", jdbcType=JdbcType.CHAR),
            @Result(column="orde_by", property="ordeBy", jdbcType=JdbcType.TINYINT)
    })
    List<SysDeptProvider> selectTreeBySid(String sid);
}